﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class searchResults : System.Web.UI.Page
{
    protected StringBuilder sb = new StringBuilder();
    protected string keyword;
    protected int count;
    protected void Page_Load(object sender, EventArgs e)
    {
        
            keyword = Request.QueryString["keyword"];
            if (!IsPostBack)
            {
            if (keyword == null)
            {
                return;
            }
            //sb.Append(keyword);
            int numbers;
            if (common.searchByKW(keyword, 0, 8, out numbers).Count != 0)
            {
                //绑定搜索得到的结果
                //List<MODEL.LostAndFound> models = common.searchByKW(keyword, 0, 8, out numbers);
                //rpSearchResults.DataSource = models;
                //rpSearchResults.DataBind();
                //pageNum.Text = "1";
                //pageCount.Text = Math.Ceiling((numbers / ((double)8))).ToString();
                Bind(1, keyword);
            }
            else
            {
                pageNum.Text = "1";
                pageCount.Text = Math.Ceiling((numbers / ((double)8))).ToString();
                sb.Append("没有找到您想要的内容");
            }
        }
    }

    protected void Bind(int pagenum,string keyword)
    {
        List<MODEL.LostAndFound> models = common.searchByKW(keyword,(pagenum-1)*8,8*pagenum-1, out count);
        rpSearchResults.DataSource = models;
        rpSearchResults.DataBind();
        if (pagenum < 1)
        {
            pagenum = 1;
        }
        int count1 =Convert.ToInt32(Math.Ceiling((count / ((double)8))));
        if (pagenum > count)
        {
            pagenum = count;
        }
        //别要忘了这个
        btnpre.Enabled = true;
        btnnext.Enabled = true;
        if (pagenum == 1)
        {
            btnpre.Enabled = false;
        }
        if (count1== pagenum)
        {
            btnnext.Enabled = false;
        }
        pageNum.Text = pagenum.ToString();
        pageCount.Text = Math.Ceiling((count / ((double)8))).ToString();
    }

    //上一页
    protected void btnpre_Click(object sender, EventArgs e)
    {
        int currentpage = Convert.ToInt32(pageNum.Text);
        currentpage--;
        Bind(currentpage, keyword);
    }
    //下一页
    protected void btnnext_Click(object sender, EventArgs e)
    {
        int currentpage = Convert.ToInt32(pageNum.Text);
        currentpage++;
        Bind(currentpage,keyword);
    }
    //跳转
    protected void btnjump_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int pagenum = Convert.ToInt32(txtjump.Text);
            if (pagenum > Convert.ToInt32(Math.Ceiling((count / ((double)8)))))
            {
                Bind(1, keyword);
            }
            else
            {
                Bind(pagenum, keyword);
            }

        }
    }
}